<?php


namespace app\zhengmaotong\controller;


use app\zhengmaotong\model\WaresModel;
use cmf\controller\AdminBaseController;

use think\facade\Db;
use think\Request;

class WaresController extends AdminBaseController
{
    // 首页
    public function index()
    {
        //获取查询数据
        $name = input('name');
        $where = [];
        //判断是否有值
        if ($name) {
            $where[] = ['name', 'like', '%'.$name.'%'];
        }
        //查询排序分页
        $list = WaresModel::order('id DESC')
            ->where($where)
            ->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }

    // 添加商品
    public function add()
    {
        $list = WaresModel::select();
        return $this->fetch('add',['list' => $list]);
    }

    public function addPost()
    {
        $data = input('post.');
        $data['images'] = implode(',', $data['images']);
        if (WaresModel::create($data)) {
            $this->success('添加成功','Wares/index');
        } else {
            $this->error('添加失败');
        }
    }

    // 修改
    public function edit()
    {
        $id = input('id');
        $list = WaresModel::find($id);
        return $this->fetch('edit',['list' => $list]);
    }

    public function editPost()
    {
        $data = input('post.');
        // 查询是否有此记录
        $list = WaresModel::find($data['id']);
        if (!$list) {
            $this->error('未查询到该记录');
        }
        if ($list->update($data)) {
            $this->success('修改成功','Wares/index');
        } else {
            $this->error('修改失败');
        }
    }

    // 删除
    public function delete()
    {
        $id = input('id/d','');
        $del = WaresModel::destroy($id);
        if($del) {
            $this->success('删除成功','Wares/index');
        } else {
            $this->error('删除失败');
        }
    }

    // 显示隐藏内容
    public function isShow(Request $request)
    {
        $id = $request->param('id');
        if(!$id) {
            $this->error('未查询到该id');
        }
        $wares = WaresModel::find($id);
        $source = null;
        if($wares['source'] == 1) {$source = 0;}
        if($wares['source'] == 0) {$source = 1;}

        if(WaresModel::update(['source'=>$source],['id'=>$id])) {
            $this->success('修改成功','Wares/index');
        } else {
            $this->error('修改失败');
        }
    }

    // 打开配置商品属性页面
    public function setGoodsAttr() {
        $goodsId = input('goods_id');
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr2');
    }
    // 配置商品属性提交
    public function setGoodsAttrPost() {
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs)-1 < 1){
            $this->error('请至少配置一个属性！');
        }
        Db::name('goods_attr')
            ->where('goods_id', $goodsId)
            ->delete();
        $insert_data = [];
        foreach($attrs as $attr){
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr['name']
            ];
        }
        Db::name('goods_attr')->insertAll($insert_data);
        $this->success('保存成功');
    }

    // 打开配置商品属性值页面
    public function setGoodsAttrValue()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id', $goodsId);
        return $this->fetch('add_goods_attr_value');
    }

    public function setGoodsAttrValuePost()
    {
            $data = input('post.form');
            $insertData = [];
            foreach($data as $attrValue){
                $insertData[] = [
                    'gid_name' => $attrValue['attr_value'],
                    'gid_id' => $attrValue['attr']
                ];
            }
            $insert = Db::name('goods_attr_value')->insertAll($insertData);
            if ($insert){
                return json(['code' => 1, 'msg' => '添加成功', 'data' => "NULL"]);
            }else{
                return json(['code'=>0, 'msg' => '添加失败', 'data' => "NULL"]);
            }
    }
    public function getGoodsAttr()
    {
        $goodsId = input('goods_id');
        $list = Db::name('goods_attr')
            ->where('goods_id',$goodsId)
            ->select();
        return json(['code'=>1, 'msg'=>'商品属性', 'data' => $list]);
    }

    public function getGoodsAttr2()
    {
        $goodsId = input('goods_id');
        $list = Db::name('goods_attr')
            ->where('goods_id',$goodsId)
            ->select();
        return json(['code'=>1, 'msg'=>'商品属性', 'data' => $list]);
    }
}